<?php
// +----------------------------------------------------------------------
// | 品达网络 [ 品质保证，助企腾达 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.pdwl.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 萧涩啊 <wangmode@163.com>
// +----------------------------------------------------------------------
// | 支出管理

namespace app\models;
use think\Exception;
use think\Model;


class Income extends BaseModel
{
    protected $table = 'sh_income';
    /**
     * @param $page
     * @param $limit
     * @param int $operation_id
     * @param int $business_id
     * @param string $keyword
     * @return array|\PDOStatement|string|\think\Collection|\think\model\Collection
     */
    public function getDataList($page,$limit,$operation_id=0,$business_id=0,$product_id=0,$keyword=''){
        try {
            $where = [];
            if($keyword!=''){
                $where['i.company'] = ['like',"%$keyword%"];
            }
            if($operation_id!=0){
                $where['i.operation_id'] = $operation_id;
            }
            if($business_id!=0){
                $where['i.business_id'] = $business_id;
            }
            return $this->alias('i')
                ->where($where)
                ->leftJoin('sh_employee e','i.operation_id=e.id')
                ->leftJoin('sh_employee e2','i.business_id=e2.id')
                ->leftJoin('sh_department d','i.department_id=d.id')
                ->field("i.*,e.name as operation,e2.name as business,d.name as department")
                ->limit(($page-1)*$limit,$limit)
                ->select();
        }catch (Exception $exception){
            return $this->setErrorInfo($exception->getMessage());
        }
    }


    /**
     * 获取收入信息
     * @param $id
     * @return array|\PDOStatement|string|Model|null
     */
    public function getInfo($id)
    {
        try {
            return $this->where('id',$id)->find();
        }catch (\Exception $exception){
            return self::setErrorInfo($exception->getMessage());
        }
    }

    /**
     * @param int $operation_id
     * @param int $business_id
     * @param string $keyword
     * @param int $product_id
     * @return float|int|string
     */
    public function getCount($operation_id=0,$business_id=0,$product_id=0,$keyword='')
    {
        $where = [];
        if($keyword!=''){
            $where['company'] = ['like',"%$keyword%"];
        }
        if($operation_id!=0){
            $where['operation_id'] = $operation_id;
        }
        if($business_id!=0){
            $where['business_id'] = $business_id;
        }
        if($product_id!=0){
            $where['product_id'] = $product_id;
        }
        return $this->where($where)->count();
    }

    /**
     * @param $id
     * @param $data
     * @return Income
     */
    public function edit($id,$data)
    {
        return $this->update($data,['id'=>$id]);
    }


}
